package com.tianyang.yukuaifu.business.trade.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.trade.adapter.SubMerchantStatAdapter;
import com.tianyang.yukuaifu.business.trade.bean.SubMerchantStatOutput;
import com.tianyang.yukuaifu.business.trade.contract.SubMerchantStatContract;
import com.tianyang.yukuaifu.business.trade.contract.TradeConfig;
import com.tianyang.yukuaifu.business.trade.presenter.SubMerchantStatPresenter;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Description：商户详情统计
 * Created by xyj on 2024/3/26
 */
public class SubMerchantStatActivity extends BaseTitleActivity implements SubMerchantStatContract.View {

    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;

    private SubMerchantHeaderView vHeaderView;

    private RelativeLayout emptyView;
    
    private SubMerchantStatAdapter vSubMerchantStatAdapter;
    
    private SubMerchantStatContract.Presenter mPresenter;

    private int nowPage = CommonConfig.PAGE_NUM;

    private String startTime, endTime;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recycler_view;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("我的商户业绩");
        String mchName = getIntent().getStringExtra(TradeConfig.INTENT_MCH_NAME);
        startTime = getIntent().getStringExtra(TradeConfig.INTENT_START_TIME);
        endTime = getIntent().getStringExtra(TradeConfig.INTENT_END_TIME);
        boolean isMonthFilter = getIntent().getBooleanExtra(TradeConfig.INTENT_IS_MONTH_FILTER, false);
        String mchPhone = getIntent().getStringExtra(TradeConfig.INTENT_MCH_PHONE);
        mPresenter = new SubMerchantStatPresenter(this, mchPhone);

        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vSubMerchantStatAdapter = new SubMerchantStatAdapter();
        vHeaderView = new SubMerchantHeaderView(mContext, null);
        emptyView = vHeaderView.findViewById(R.id.emptyView);
        vHeaderView.setCallbackListener(new ICallbackListener<String>() {
            @Override
            public void callback(String value) {
                if (!TextUtils.isEmpty(value) && value.contains(",")) {
                    String[] values = value.split(",");
                    startTime = values[0];
                    endTime = values[1];
                    mPresenter.posMerchantTradeDetail(nowPage, startTime, endTime);
                }
            }
        });
        vHeaderView.initData(mchName, startTime, endTime, isMonthFilter);
        vSubMerchantStatAdapter.addHeaderView(vHeaderView);
        vRecyclerview.setAdapter(vSubMerchantStatAdapter);
        vSubMerchantStatAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vSubMerchantStatAdapter.setPreLoadNumber(2);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.posMerchantTradeDetail(nowPage, startTime, endTime);
                vSubMerchantStatAdapter.setEnableLoadMore(true);
            }
        });
        vSubMerchantStatAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.posMerchantTradeDetail(nowPage, startTime, endTime);
            }
        }, vRecyclerview);
    }

    @Override
    public void showMerchantTradeStatView(JSONObject jsonObject) {
        vHeaderView.showDataView(jsonObject);
    }

    @Override
    public void showMerchantTradeListView(List<SubMerchantStatOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vSubMerchantStatAdapter.setNewData(outputs);
            } else {
                vSubMerchantStatAdapter.addData(outputs);
            }
            nowPage++;
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vSubMerchantStatAdapter.setNewData(new ArrayList<>());
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vSubMerchantStatAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vSubMerchantStatAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vSubMerchantStatAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(SubMerchantStatContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
